<!DOCTYPE html>
<html lang="en">
<head>
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no,width=device-width,height=device-height">
  <meta charset="UTF-8">
  <meta name="Author" content="haley">
  <meta name="Keywords" content="前端最常用的UI插件">
  <meta name="Description" content="最常用的UI插件">
  <script crossorigin="anonymous" integrity="sha384-6ePHh72Rl3hKio4HiJ841psfsRJveeS+aLoaEf3BWfS+gTF0XdAqku2ka8VddikM" src="https://lib.baomitu.com/jquery/1.11.3/jquery.min.js"></script>
  <link crossorigin="anonymous" integrity="sha384-pdapHxIh7EYuwy6K7iE41uXVxGCXY0sAjBzaElYGJUrzwodck3Lx6IE2lA0rFREo" href="https://lib.baomitu.com/twitter-bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
  <link href="../../css/page.css" rel="stylesheet">
  <script crossorigin="anonymous" integrity="sha384-pPttEvTHTuUJ9L2kCoMnNqCRcaMPMVMsWVO+RLaaaYDmfSP5//dP6eKRusbPcqhZ" src="https://lib.baomitu.com/twitter-bootstrap/3.3.5/js/bootstrap.min.js"></script>
  <script crossorigin="anonymous" integrity="sha384-8t+aLluUVnn5SPPG/NbeZCH6TWIvaXIm/gDbutRvtEeElzxxWaZN+G/ZIEdI/f+y" src="https://lib.baomitu.com/vue/2.6.10/vue.min.js"></script>
  <script src="../../js/page.js"></script>
  <script src="../../js/changeTitle.js"></script>
  <link href="../../imgs/favicon.ico" rel="shortcut icon">
  <script src="../../js/goToHome.js"></script>
  <title></title>
  <script>
    var data={
      title:'浮动模块，如何保持保持高度一致？',
      content:{
        intro:{
          list:[
            {
              title:'使用说明：',
              className:'bs-callout-info',
              list:[
                {title:'基础配置：',content:'jq'}
                ,{title:'需要的代码：',content:'核心js代码，核心html部分代码和style代码只为演示方便'}
                ,{title:'如何使用：',content:'直接复制最下面的js源码'}
                ,{title:'API：',content:'setSameHeight({cols:cols,item:item,itemH:itemH,height:height}) cols:列数，item：被设置高度的项，itemH：高度来源，height:高度数值；优先级：高度 > itemH > item'}
              ]
            }
            ,{
              title:'实现原理：',
              className:'bs-callout-warning',
              list:[
                {
                  title:'',
                  content:'找到同行内最高的高度，作为所有的高度'
                }
              ]
            }
            ,{
              title:'问题描述：',
              className:'bs-callout-warning',
              list:[
                {
                  title:'为何存在：',
                  content:'有时候在做多个模块浮动布局时，由于某个模块比其他的高，会使后面紧挨着他的模块被“卡住”，导致页面错乱'
                }
              ]
            }
          ]
        }
      }
    };
  </script>
  <style>
    .fb{
      font-weight: bold;
    }
    #detail1{
      margin-right: 0;
    }
    #detail1,#detail2{
    }
    .detailModal__item{
      border:1px solid #00a0e9;
    }
    #detail1 .detailModal__item:nth-child(4n+3),#detail1 .detailModal__item:nth-child(4n+4){
      background-color: lightgreen;
    }
    #detail2 .detailModal__item:nth-child(6n+4),#detail2 .detailModal__item:nth-child(6n+5),#detail2 .detailModal__item:nth-child(6n+6){
      background-color: lightgreen;
    }
  </style>
</head>
<body>
<div id="page">
  <div class="container">
    <h3 class="text-center">{{title}}</h3>
    <div class="bs-callout" :class="item.className" v-for="item in content.intro.list">
      <h4>{{item.title}}</h4>
      <ul>
        <li v-for="item2 in item.list"><b>{{item2.title}}</b>{{item2.content}}</li>
      </ul>
    </div>
  </div>
  <script>
    var page = new Vue({
      el: '#page',
      data: data
    });
  </script>
</div>

<div class="container">
  <section class="panel panel-info">
    <div class="panel-heading">浮动模块，如何保持保持高度一致？</div>
    <div class="panel-body">
      <!--核心html代码 s-->
      <div class="row">
        <div class="col-sm-12">
          <div class="row">
            <div class="col-sm-12">
              <img src="img/sameH.png" alt="" width="100%"/>
              <p class="tc">由于图片高度不同，第四张图被卡主了</p>
            </div>
          </div>
          <div class="row list" id="list1">
            <h3 class="tc">通过高度数值140 进行设置</h3>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img1"/>
                <p class="tc">120px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img2"/>
                <p class="tc">110px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img3"/>
                <p class="tc">60px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img4"/>
                <p class="tc">120px</p>
              </a>
            </div>
          </div>
          <div class="row list" id="list2">
            <h3 class="tc">通过itemH来源 进行设置</h3>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img1"/>
                <p class="tc">120px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img2"/>
                <p class="tc">110px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img3"/>
                <p class="tc">60px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img4"/>
                <p class="tc">120px</p>
              </a>
            </div>
          </div>
          <div class="row list" id="list3">
            <h3 class="tc">通过item的高度 进行设置</h3>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img1"/>
                <p class="tc">120px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img2"/>
                <p class="tc">110px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img3"/>
                <p class="tc">60px</p>
              </a>
            </div>
            <div class="col-sm-4">
              <a href="#">
                <img src="img/test.jpg" alt="" class="img img4"/>
                <p class="tc">120px</p>
              </a>
            </div>
          </div>
        </div>
      </div>
      <style>
        .tc{
          text-align: center;
        }
        .list>div{
          border:1px solid #00a0e9;
        }
        .img{
          width: 100%;

        }
        .img1{
          height: 120px;
        }
        .img2{
          height: 110px;
        }
        .img3{
          height: 60px;
        }
        .img4{
          height: 120px;
        }
      </style>
      <!--核心html代码 e-->
    </div>
  </section>
</div>
<!--核心js-->
<script>
  $(function () {
    function setSameHeight(config){
      var arrH1=[],arrH2=[];
      if(config.height&&config.height!=0){
        $(config.item).each(function(index,item){
          $(item).height(config.height);
        });
      }else if(config.itemH){
        console.log(12);
        $(config.itemH).each(function(index,item){
          arrH1.push($(item).height());
        });
        for(var i= 0,len=arrH1.length;i<len;i+=config.cols){
          for(var j= 0;j<config.cols;j++){
            arrH2.push(Math.max(arrH1[i],arrH1[i+1]));
          }
        }
        $(config.item).each(function(index,item){
          $(item).height(arrH2[index]);
        });
      }else{
        $(config.item).each(function(index,item){
          arrH1.push($(item).height());
        });
        for(var i= 0,len=arrH1.length;i<len;i+=config.cols){
          for(var j= 0;j<config.cols;j++){
            arrH2.push(Math.max(arrH1[i],arrH1[i+1]));
          }
        }
        $(config.item).each(function(index,item){
          $(item).height(arrH2[index]);
        });
      }
    }
    // 一行内不同列的高度一致
    /**
     * 源数据优先级：高度 > itemH > item
     * 依次从前往后找数据
     * */

    setSameHeight({
      cols:3,
      item:'#list1>div',
      itemH:'#list1 img',
      height:140
    });
    setSameHeight({
      cols:3,
      item:'#list2>div',
      itemH:'#list2 img',
      height:0
    });
    setSameHeight({
      cols:3,
      item:'#list3>div',
      itemH:'',
      height:0
    });
  });
</script>
<!--核心js-->
<div class="container">
  <section class="panel panel-info">
    <div class="panel-heading">源码</div>
    <div class="panel-body">
      <!--核心代码 s-->
      <div class="row">
        <div class="col-sm-12">
          <div class="">
            <p>
<pre>
$(function () {
    function setSameHeight(config){
      var arrH1=[],arrH2=[];
      if(config.height&&config.height!=0){
        $(config.item).each(function(index,item){
          $(item).height(config.height);
        });
      }else if(config.itemH){
        console.log(12);
        $(config.itemH).each(function(index,item){
          arrH1.push($(item).height());
        });
        for(var i= 0,len=arrH1.length;i&lt;len;i+=config.cols){
          for(var j= 0;j&lt;config.cols;j++){
            arrH2.push(Math.max(arrH1[i],arrH1[i+1]));
          }
        }
        $(config.item).each(function(index,item){
          $(item).height(arrH2[index]);
        });
      }else{
        $(config.item).each(function(index,item){
          arrH1.push($(item).height());
        });
        for(var i= 0,len=arrH1.length;i&lt;len;i+=config.cols){
          for(var j= 0;j&lt;config.cols;j++){
            arrH2.push(Math.max(arrH1[i],arrH1[i+1]));
          }
        }
        $(config.item).each(function(index,item){
          $(item).height(arrH2[index]);
        });
      }
    }
    // 一行内不同列的高度一致
    /**
     * 源数据优先级：高度 > itemH > item
     * 依次从前往后找数据
     * */

    setSameHeight({
      cols:3,
      item:'#list1>div',
      itemH:'#list1 img',
      height:140
    });
    setSameHeight({
      cols:3,
      item:'#list2>div',
      itemH:'#list2 img',
      height:0
    });
    setSameHeight({
      cols:3,
      item:'#list3>div',
      itemH:'',
      height:0
    });
  });
</pre>
            </p>
          </div>

        </div>
      </div>
      <!--核心代码 e-->
    </div>
  </section>
</div>
</body>

</html>
